<template>
  <div class="item-con">
    <div class="num question-item">第<span class="remain">{{ itemNum + 1 }}</span> 题</div>
    <div class="title question-item">{{ problems[itemNum].problemTitle }} <span class="triangle"></span></div>
    <div class="question-lists">
      <li
        v-for="(item, key) in problems[itemNum].answers"
        :key="key"
      ><input
          v-model="chooseId"
          type="radio"
          :value="item"
          :id="item.answerId"
          name="answer"
        ><label :for="item.answerId">{{ item.text }}</label></li>
    </div>
    <div class="btns">
      <button
        class="red-bg next-btn"
        @click="nextItem"
        v-if="itemNum < problems.length-1"
      >下一题</button>
      <button
        class="green-bg done-btn"
        @click="postAll"
        v-else
      >交卷</button>
    </div>
    <modal v-if="isDone">
      <done-modal></done-modal>
    </modal>
  </div>
</template>
<script>
import { mapState, mapActions } from "vuex";
import modal from "../components/modal";
import doneModal from "./doneModal";
export default {
  components: {
    modal: modal,
    "done-modal": doneModal
  },
  data() {
    return {
      chooseId: null,
      allScore: null
    };
  },
  created() {
    this.initData();
  },
  mounted() {},
  computed: {
    ...mapState([
      "itemNum",
      "allTime",
      "timer",
      "isDone",
      "problems",
      "answerId",
      "resultScore"
    ])
  },
  methods: {
    ...mapActions(["initData", "addItem"]),
    // 下一步
    nextItem() {
      if (this.chooseId) {
        let obj = {};
        obj.answerId = this.chooseId.answerId;
        obj.fid = this.chooseId.fid;
        this.chooseId = null;
        // action提交
        this.addItem(obj);
      } else {
        this.$toast("请选择");
      }
    },
    postAll() {
      if (this.chooseId) {
        let obj = {};
        obj.answerId = this.chooseId.answerId;
        obj.fid = this.chooseId.fid;
        this.chooseId = null;
        // action提交
        this.addItem(obj);
        // 最后一题清除定时器
        clearInterval(this.timer);
        this.$store.commit("CLEAR_TIME");
        this.$store.commit("IS_DONE", true);
      } else {
        this.$toast("请选择");
      }
    }
  }
};
</script>

<style>
.item-con {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  padding: 1.5rem 1rem;
}
.item-con .question-item {
  width: 100%;
  border: 1px solid #dbdbdb;
  padding: 0.35rem;
  box-sizing: border-box;
  font-size: 14px;
  text-align: center;
  position: relative;
}
.item-con .question-item:first-child {
  border-bottom: 0;
}
.item-con .num .remain {
  padding: 0.1rem 0.2rem;
  background: #dd4b4b;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  color: #fff;
  margin-left: 0.1rem;
}
.item-con .triangle {
  display: block;
  width: 0;
  height: 0;
  border-width: 10px;
  border-style: solid;
  border-color: #dbdbdb transparent transparent transparent;
  position: absolute;
  bottom: -20px;
  left: 50%;
  margin-left: -10px;
}
.item-con .triangle:after {
  content: "";
  display: block;
  width: 0;
  height: 0;
  border-width: 10px;
  border-style: solid;
  border-color: #fff transparent transparent transparent;
  position: absolute;
  top: -11px;
  left: -10px;
}
.question-lists {
  margin-top: 30px;
}
.question-lists li {
  display: flex;
  line-height: 40px;
  align-items: center;
}
.question-lists input[type="radio"] {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  width: 0.5rem;
  height: 0.5rem;
  outline: 0;
  margin-right: 0.25rem;
  background: url("")
    no-repeat;
  -webkit-background-size: 100% 100%;
  background-size: 100% 100%;
}
.question-lists input[type="radio"]:checked {
  background: url("")
    no-repeat;
  -webkit-background-size: 100% 100%;
  background-size: 100% 100%;
}
.btns {
  margin-top: 0.5rem;
  text-align: center;
}
.btns button {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border: 0;
  -webkit-border-radius: 0.5rem;
  -moz-border-radius: 0.5rem;
  border-radius: 0.5rem;
  width: 2.3rem;
  height: 1rem;
  color: #fff;
}

.pass-chance-num {
  text-align: center;
  margin-top: 0.5rem;
  color: #666;
}
</style>
